####################################################################
## author:    Robert A. Huber
## contact:   robert.huber@ir.gess.ethz.ch
## file name: pc_uk_factoranalysis.R
## Context:   Populism and Climate Sceptism, individuals from BES
## started:   2016-10-12
## Summary:   Runs Factor Analysis for Populism and Political Efficacy
######################################################################

df_efficacy <- data.frame(df_bes$efficacyPolCare, df_bes$efficacyNoMatter, df_bes$efficacyNotUnderstand, df_bes$efficacyUnderstand, df_bes$efficacyTooMuchEffort)

df_efficacy$df_bes.efficacyPolCare <- ifelse(df_efficacy$df_bes.efficacyPolCare == "Don't know", NA,
                                             as.numeric(df_efficacy$df_bes.efficacyPolCare))

df_efficacy$df_bes.efficacyNoMatter <- ifelse(df_efficacy$df_bes.efficacyNoMatter == "Don't know", NA,
                                              as.numeric(df_efficacy$df_bes.efficacyNoMatter))

df_efficacy$df_bes.efficacyNotUnderstand <- ifelse(df_efficacy$df_bes.efficacyNotUnderstand == "Don't know", NA,
                                                   as.numeric(df_efficacy$df_bes.efficacyNotUnderstand))

df_efficacy$df_bes.efficacyUnderstand <- ifelse(df_efficacy$df_bes.efficacyUnderstand == "Don't know", NA,
                                                as.numeric(df_efficacy$df_bes.efficacyUnderstand))

df_efficacy$df_bes.efficacyTooMuchEffort <- ifelse(df_efficacy$df_bes.efficacyTooMuchEffort == "Don't know", NA,
                                                   as.numeric(df_efficacy$df_bes.efficacyTooMuchEffort))

alpha(df_efficacy)

fa.parallel(df_efficacy, fa="fa")
## Test to assess the number of factors with 5 different rule:
#1 Only Factors above eigenvalue 1
#2 Only Factors before the "elbow": Strong difference beween Factor i and i+1
#3 Only Factors above Simulated or resampled data
VSS(df_efficacy, 3, rotate="promax", fm="mle")
#4 Only number of factors close to CSS Complexity 1 and 2
#5 Only number of factors mentioned by the MAP criterion

fa_efficacy <- fa(df_efficacy, 1, fm="ml", rotate = "varimax")
#Factor analysis by psych package. fm="ml" == factanal in R

print(fa_efficacy, digits = 2, cut = .3, sort = TRUE)
#Output from Factor analysis. Round for 2 digits and no loadings below .2 sorted by loading

hist(fa_efficacy$scores)

df_bes$efficacy <- fa_efficacy$scores

# Factor Analysis: Populist Attitudes -------------------------------------

df_pop <- data.frame(df_bes$populism1, df_bes$populism2, df_bes$populism4, df_bes$populism5, df_bes$populism6)

df_pop$df_bes.populism1 <- ifelse(df_pop$df_bes.populism1 == "Don't know", NA,
                                  as.numeric(df_pop$df_bes.populism1))

df_pop$df_bes.populism2 <- ifelse(df_pop$df_bes.populism2 == "Don't know", NA,
                                  as.numeric(df_pop$df_bes.populism2))

df_pop$df_bes.populism4 <- ifelse(df_pop$df_bes.populism4 == "Don't know", NA,
                                  as.numeric(df_pop$df_bes.populism4))

df_pop$df_bes.populism5 <- ifelse(df_pop$df_bes.populism5 == "Don't know", NA,
                                  as.numeric(df_pop$df_bes.populism5))

df_pop$df_bes.populism6 <- ifelse(df_pop$df_bes.populism6 == "Don't know", NA,
                                  as.numeric(df_pop$df_bes.populism6))

summary(alpha(df_pop))
## Alpha gives us the coherency between these items

fa.parallel(df_pop, fa="fa")
## Test to assess the number of factors with 5 different rule:
#1 Only Factors above eigenvalue 1
#2 Only Factors before the "elbow": Strong difference beween Factor i and i+1
#3 Only Factors above Simulated or resampled data
VSS(df_pop, 3, rotate="promax", fm="mle")
#4 Only number of factors close to CSS Complexity 1 and 2
#5 Only number of factors mentioned by the MAP criterion

fa_pop <- fa(df_pop, 1, fm="ml", rotate = "varimax")
#Factor analysis by psych package. fm="ml" == factanal in R

print(fa_pop, digits = 2, cut = .5, sort = TRUE)
#Output from Factor analysis. Round for 2 digits and no loadings below .2 sorted by loading

hist(fa_pop$scores)

df_bes$populism <- fa_pop$scores

str(fa_pop$loadings)
class(fa_pop$loadings)

fa_out_pop <- xtable(unclass(fa_pop$loadings))




# AUTH --------------------------------------------------------------------

df_auth <- data.frame(df_bes$auth1, df_bes$auth2, df_bes$auth3, df_bes$auth4)

df_auth$df_bes.auth1 <- ifelse(df_auth$df_bes.auth1 == "Don't know", NA,
                                             as.numeric(df_auth$df_bes.auth1)-1)

df_auth$df_bes.auth2 <- ifelse(df_auth$df_bes.auth2 == "Don't know", NA,
                                              abs(as.numeric(df_auth$df_bes.auth2)-2))

df_auth$df_bes.auth3 <- ifelse(df_auth$df_bes.auth3 == "Don't know", NA,
                               abs(as.numeric(df_auth$df_bes.auth3)-1))

df_auth$df_bes.auth4 <- ifelse(df_auth$df_bes.auth4 == "Don't know", NA,
                               abs(as.numeric(df_auth$df_bes.auth4)-1))

alpha(df_auth)

fa.parallel(df_auth, fa="fa")
## Test to assess the number of factors with 5 different rule:
#1 Only Factors above eigenvalue 1
#2 Only Factors before the "elbow": Strong difference beween Factor i and i+1
#3 Only Factors above Simulated or resampled data
VSS(df_auth, 3, rotate="promax", fm="mle")
#4 Only number of factors close to CSS Complexity 1 and 2
#5 Only number of factors mentioned by the MAP criterion

fa_auth <- fa(df_auth, 1, fm="ml", rotate = "varimax")
#Factor analysis by psych package. fm="ml" == factanal in R

print(fa_auth, digits = 2, cut = .3, sort = TRUE)
#Output from Factor analysis. Round for 2 digits and no loadings below .2 sorted by loading

hist(fa_auth$scores)

df_bes$auth <- fa_auth$scores


str(fa_auth$loadings)
class(fa_auth$loadings)

fa_out_auth <- xtable(unclass(fa_auth$loadings))

rm(list=setdiff(ls(), c("df_bes", "fa_out_pop", "fa_out_auth")))
